// pages/xq/xq.js
import http from "../../request/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "0",
    banner: [],
    text: "",
    list: {},
    flag: false,
    goods_id: 0

  },
  letlike(e) {
    let arr = wx.getStorageSync('like') || []
    let index = arr.findIndex(item => {
      return item.id == e.message.goods_id
    })
    if (index > -1) {
      this.setData({
        flag: true
      })
    } else {
      this.setData({
        flag: false
      })
    }

  },
  like() {
    if (this.data.flag == false) {
      let arr = wx.getStorageSync('like') || []
      // let index = arr.findIndex(item => {
      //   return item.id == this.data.list.goods_id
      // })
      this.setData({
        flag: true
      })
      arr.push({
        name: this.data.list.goods_name,
        price: this.data.list.goods_price,
        img: this.data.list.goods_big_logo,
        id: this.data.list.goods_id,
      })
      wx.setStorageSync('like', arr)
      wx.showToast({
        title: '收藏成功',
        icon: "success"
      })
    } else {
      wx.showModal({
        cancelColor: 'cancelColor',
        title: "您确定取消收藏吗？",
        confirmText: "是",
        cancelText: "否",
        cancelColor: "#000",
        success: (res => {
          if (res.confirm) {
            let arr = wx.getStorageSync('like') || []
            let index = arr.findIndex(item => {
              return item.id == this.data.list.goods_id
            })
            arr.splice(index, 1)
            wx.setStorageSync('like', arr)
            this.setData({
              flag: false
            })
            wx.showToast({
              title: '取消收藏',
              icon: "success"
            })

          }
          if (res.cancel) {
            console.log("不是");
          }
        })
      })
    }
    // this.setData({
    //   flag: !this.data.flag
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.setData({
      id: e.id
    })
    this.getlist()

  },
  gwc() {
    wx.switchTab({
      url: '../gwc/gwc'
    })
  },
  getlist() {
    http("public/v1/goods/detail", {
      mmethod: "get",
      data: {
        goods_id: this.data.id
      }
    }).then(res => {
      let {
        data
      } = res
      this.letlike(data)
      if (data.meta.status == 200) {
        this.setData({
          banner: data.message.pics,
          price: data.message.goods_price,
          name: data.message.goods_name,
          text: data.message.goods_introduce,
          list: data.message,
          goods_id: data.message.goods_id
        })
      }
    })
  },
  add() {
    let arr = wx.getStorageSync('list') || []
    let index = arr.findIndex(item => {
      return item.id == this.data.list.goods_id
    })
    if (index > -1) {
      arr[index].num++
      wx.showToast({
        title: '数量' + arr[index].num,
      })

    } else {
      arr.push({
        name: this.data.list.goods_name,
        num: 1,
        price: this.data.list.goods_price,
        img: this.data.list.goods_big_logo,
        id: this.data.list.goods_id,
        checked: false
      })
      wx.showToast({
        title: '加入成功',
      })
    }
    wx.setStorageSync('list', arr)

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})